/*
 * $Id$
 *
 * Copyright 2008 HuangHuang. All rights reserved.
 */

package com.crm.web.page;

import org.apache.tapestry.html.BasePage;
import org.apache.tapestry.annotations.InjectState;
import org.apache.tapestry.annotations.InjectPage;
import org.apache.tapestry.form.IPropertySelectionModel;
import org.apache.tapestry.form.StringPropertySelectionModel;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.IPage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.javaforge.tapestry.spring.annotations.InjectSpring;
import com.crm.service.IContactService;
import com.crm.model.User;
import com.crm.model.Contact;

import java.util.List;

/**
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version $Id$
 */
public abstract class ContactSearch extends BasePage implements PageBeginRenderListener {
    private static Log log = LogFactory.getLog(ContactSearch.class);

    @InjectSpring("contactService")
    public abstract IContactService getContactService();

    @InjectState("userSession")
    public abstract User getUserSession();

    @InjectPage("ContactModify")
    public abstract ContactModify getContactModifyPage();

    public abstract String getKeyword();

    public abstract String getFieldSearch();

    /**
     * forward to page SysPageError if user session is null
     *
     * @param event Encapsulates information related to the page listener interfaces.
     */
    public void pageBeginRender(PageEvent event) {
        if (getUserSession().getUserCode() == null)
            event.getRequestCycle().sendRedirect("app?page=SysPageError&service=page");
    }

    /**
     * Constructs a options of the drop-down list.
     *
     * @return a StringPropertySelectionModel of implemented IPropertySelectionModel.
     */
    public IPropertySelectionModel getFieldSearchModel() {
        return new StringPropertySelectionModel(new String[]{"所属客户", "联系人", "邮箱"});
    }

    /**
     * Load contact information list for page.
     *
     * @return A list.
     */
    public List getEntries() {
        try {
            String keyword = getKeyword() == null ? getKeyword() : getKeyword().trim();
            return getContactService().getContacts(getUserSession(), new String[]{keyword, getFieldSearch()});
        }
        catch (Exception e) {
            log.error(getUserSession().getUsername()
                    + ": The error happened when page ContactSearch loading!\n"
                    + e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    public void searchByKeyword() {
    }

    public IPage onShowDetails(Contact contact) {
        ContactModify page = getContactModifyPage();
        page.setContactInfo(contact);
        page.setSourcePage(this.getPageName());
        return page;
    }
}